package com.bwsoft.api.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.bwsoft.action.BaseAction;
import com.bwsoft.admin.dao.FileDao;
import com.bwsoft.api.service.CodeService;
import com.bwsoft.api.service.DictionaryService;
import com.bwsoft.api.service.NewsService;
import com.bwsoft.api.service.QualityControlService;
import com.bwsoft.api.service.ReportService;
import com.bwsoft.api.service.StandService;
import com.bwsoft.api.service.UserService;
import com.bwsoft.api.util.Constant;
import com.bwsoft.util.Base64Util;
import com.bwsoft.util.DesUtil;

public class ApiBaseAction<T> extends BaseAction {

	private static Logger LOG = Logger.getLogger(ApiBaseAction.class);

	@Resource(name = NewsService.NAME)
	public NewsService newsService;

	@Resource(name = UserService.NAME)
	public UserService userService;
	@Resource(name = CodeService.NAME)
	public CodeService codeService;
	@Resource(name = QualityControlService.NAME)
	public QualityControlService qualityControlService;

	@Resource(name = ReportService.NAME)
	public ReportService reportService;
	@Resource(name = StandService.NAME)
	public StandService standService;
	@Resource(name = DictionaryService.NAME)
	public DictionaryService dictionaryService;

	@Resource(name = FileDao.NAME)
	public FileDao fileDao;

	long beginTimestamp;

	File[] img; // 上传的文件
	String[] imgFileName; // 文件名称
	String[] imgContentType; // 文件类型
	
	File img1; // 上传的文件
	String img1FileName; // 文件名称
	String img1ContentType; // 文件类型
	
	File img2; // 上传的文件
	String img2FileName; // 文件名称
	String img2ContentType; // 文件类型
	
	File img3; // 上传的文件
	String img3FileName; // 文件名称
	String img3ContentType; // 文件类型

	String r; // DES加密参数名

	// ****************************//
	// //
	// 自定义方法 //
	// //
	// ***************************//
	/**
	 * 解析参数转换成Map
	 * 
	 * @return
	 * @throws IOException
	 * @throws Exception
	 */
	Map<String, Object> reflexParameters() throws IOException, Exception {

		if (r == null || r.isEmpty())
			return new HashMap<String, Object>();

		// 先用Base64解密然后用DES解密
		String parameters = DesUtil.decrypt(Base64Util.getFromBase64(r),
				Constant.KEY_DES);

		// 得到的解密字符串用&分隔参数
		String[] str = parameters.split("&");

		Map<String, Object> parameMap = new HashMap<String, Object>();

		StringBuffer sb = new StringBuffer("传入参数:");
		try {
			for (String string : str) {
				String key = string.split("=")[0];
				String value = string.split("=").length == 2 ? string
						.split("=")[1] : "";
				parameMap.put(key, value);
				sb.append("【" + key + "=" + value + "】;");
			}
		} catch (Exception e) {
			LOG.info("参数【" + parameters + "】异常：" + e);
			throw e;
		}
		LOG.info(sb.toString());
		return parameMap;
	}

	void finishMethod() throws IOException, Exception {
		String jsonString = JSONObject.fromObject(responseJson).toString();
		LOG.info("返回JSON:" + JSONObject.fromObject(responseJson));
		String ciphertext = Base64Util.getBase64(DesUtil.encrypt(jsonString,
				Constant.KEY_DES).getBytes());
		LOG.info("加密后的JSON：" + ciphertext);
		LOG.info("共耗时："
				+ (Calendar.getInstance().getTimeInMillis() - beginTimestamp)
				+ "ms");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println(ciphertext);
		out.flush();
		out.close();
	}
	
	

	public File[] getImg() {
		return img;
	}

	public void setImg(File[] img) {
		this.img = img;
	}

	public String[] getImgFileName() {
		return imgFileName;
	}

	public void setImgFileName(String[] imgFileName) {
		this.imgFileName = imgFileName;
	}

	public String[] getImgContentType() {
		return imgContentType;
	}

	public void setImgContentType(String[] imgContentType) {
		this.imgContentType = imgContentType;
	}

	public String getR() {
		return r;
	}

	public void setR(String r) {
		this.r = r;
	}

	public File getImg1() {
		return img1;
	}

	public void setImg1(File img1) {
		this.img1 = img1;
	}

	public String getImg1FileName() {
		return img1FileName;
	}

	public void setImg1FileName(String img1FileName) {
		this.img1FileName = img1FileName;
	}

	public String getImg1ContentType() {
		return img1ContentType;
	}

	public void setImg1ContentType(String img1ContentType) {
		this.img1ContentType = img1ContentType;
	}

	public File getImg2() {
		return img2;
	}

	public void setImg2(File img2) {
		this.img2 = img2;
	}

	public String getImg2FileName() {
		return img2FileName;
	}

	public void setImg2FileName(String img2FileName) {
		this.img2FileName = img2FileName;
	}

	public String getImg2ContentType() {
		return img2ContentType;
	}

	public void setImg2ContentType(String img2ContentType) {
		this.img2ContentType = img2ContentType;
	}

	public File getImg3() {
		return img3;
	}

	public void setImg3(File img3) {
		this.img3 = img3;
	}

	public String getImg3FileName() {
		return img3FileName;
	}

	public void setImg3FileName(String img3FileName) {
		this.img3FileName = img3FileName;
	}

	public String getImg3ContentType() {
		return img3ContentType;
	}

	public void setImg3ContentType(String img3ContentType) {
		this.img3ContentType = img3ContentType;
	}

}
